/**
 * @Type.as
 *
 * @author sodaChen mail:asframe#163.com
 * @version 1.0
 * <br>Copyright (C), 2012 ASFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:ASFrame
 * <br>Date:2012-2-13
 */
package com.asframe.ref
{
	import com.asframe.collections.maps.HashMap;
	import com.asframe.utils.ClassUtils;
	import com.asframe.utils.NamedUtils;

	import flash.system.ApplicationDomain;
	import flash.utils.getQualifiedClassName;

	/**
	 * 一个描述信息
	 * 获取方法和属性可以进行优化，额外存放在map里，这样方便集合提取，不用遍历数组
	 * @author sodaChen
	 * Date:2012-2-13
	 */
	public class Type extends MetadataContainer
	{
		public var applicationDomain			:ApplicationDomain
		/** 全名，包含了包路径和类名 **/
		public var fullName						:String;
		/** 小名字(即类名的首字母小写，正式类名是name属性) **/
		public var minName						:String;
		/**  **/
		public var base							:String;
		/** 别名 **/
		public var alias						:String;
		/** 是否动态类 **/
		public var isDynamic					:Boolean;
		/** 是否Final类 **/
		public var isFinal						:Boolean;
		/** 是否静态类 **/
		public var isStatic						:Boolean;
		/** 是否有接口 **/
		public var isInterface					:Boolean;
		/** Calss对象 **/
		public var clazz						:Class;
		/** 静态变量 **/
		public var staticVariables				:Vector.<Variable>;
		/** 静态常量 **/
		public var staticConstants				:Vector.<Variable>;
		/** 类变量 **/
		public var variables					:Vector.<Variable>;
		/** 公有变量集合，key是name **/
		public var variableMap					:HashMap;
		/** 类常量 **/
		public var constants					:Vector.<Variable>;
		/** 继承的类 **/
		public var extendsClasses				:Vector.<String>;
		/** 拥有的接口 **/
		public var interfaces					:Vector.<String>;
		/** 公有方法 **/
		public var methods						:Vector.<Method>;
		/** 公有方法集合，key是name, value:Method **/
		public var methodMap					:HashMap;
		/** 构造器对象 **/
		public var constructor					:Constructor;
		/** set/get访问符  **/
		public var accessors					:Vector.<Accessor>;

		private static var classXMLParse:ClassXMLParse = new ClassXMLParse();

		public function Type(applicationDomain	:ApplicationDomain)
		{
			this.applicationDomain = applicationDomain;
		}
		/**
		 * 初始化类型的相关数据，方便外面进行获取
		 */
		public function initType():void
		{
			variableMap = new HashMap();
			for each (var variable:Variable in variables)
			{
				variableMap.put(variable.name,variable);
			}
			methodMap = new HashMap();
			for each (var method:Method in methods)
			{
				methodMap.put(method.name,variable);
			}
		}
		/**
		 * 根据方法名称获取到对应的方法实例
		 * @param methodName
		 * @return
		 *
		 */
		public function getMethod(methodName:String):Method
		{
//			return NamedUtils.findIteam(methods,methodName);
			return methodMap.get(methodName);
		}
		/**
		 * 根据变量的名称获取到对应的变量
		 * @param variableName
		 * @return
		 *
		 */
		public function getVariable(variableName:String):Variable
		{
//			var variable:Variable = NamedUtils.findIteam(variables,variableName);
//			var variable:Variable = variableMap.get(variableName);
//			if(variable == null)
//			{
//				variable = NamedUtils.findIteam(staticVariables,variableName);
//			}
//			return variable;
			return variableMap.get(variableName);
		}
		/**
		 *
		 * @param accessorName
		 * @return
		 *
		 */
		public function getAccessor(accessorName:String):Accessor
		{
			return NamedUtils.findIteam(accessors,accessorName);
		}
		/**
		 * 根据类的全路径名称获取到对应的类描述
		 * @param name
		 * @param applicationDomain
		 * @return
		 *
		 */
		public static function forName(name:String, applicationDomain:ApplicationDomain = null):Type
		{
			return Type.forClass(ClassUtils.forName(name, applicationDomain), applicationDomain);
		}
		/**
		 * 根据Class获取到对应的类描述
		 * @param clazz
		 * @param applicationDomain
		 * @return
		 *
		 */
		public static function forClass(clazz:Class, applicationDomain:ApplicationDomain = null):Type
		{
			applicationDomain = (applicationDomain == null) ? ApplicationDomain.currentDomain : applicationDomain;
			return classXMLParse.getType(clazz, applicationDomain);
		}
		public static function forInstance(instance:Object, applicationDomain:ApplicationDomain = null):Type
		{
			return forName(getQualifiedClassName(instance),applicationDomain);
		}
	}
}
